/**
 * 事件管理器
 */
function sEvents(){
    var events = {};

    /**
     * 注册事件
     * @param {*} eventName 事件名称
     * @param {*} option 可选项
     */
    this.addEventListener = function(eventName, option){
        if(!eventName || !option) return;

        if(!events[eventName]){
            events[eventName] = {};
        }

        if(option.element){
            let name = option.name ? option.name : option.element.name;
            if(name){
                let key = name + "_" + option.element._sType;
                if(!events[eventName][key]){
                    events[eventName][key] = [];
                }
                events[eventName][key].push({
                    element: option.element
                })
            }
        }
    }

    /**
     * 分发事件
     * @param {*} eventName 事件名称
     * @param {*} option 可选项
     */
    this.dispatchEvent = function(eventName, option){
        if(!events[eventName] || !option) return;
        
        if(option.element){
            let name = option.name ? option.name : option.element.name;
            if(name){
                let key = name + "_" + option.element._sType;
                if(!events[eventName][key]) return;

                let arr = events[eventName][key];
                for(var i in arr){
                    if(arr[i].element === option.element) continue;
                    arr[i].element.dispatchEvent(eventName, option);
                }
            }
        }
    }

    /**
     * 注销事件
     * @param {*} eventName 事件名称
     * @param {*} option 可选项
     */
    this.removeEventListener = function(eventName, option){
        if(!events[eventName] || !option) return;
        if(option.element){
            let name = option.name ? option.name : option.element.name;
            if(name){
                let key = name + "_" + option.element._sType;
                if(!events[eventName][key]) return;

                let arr = events[eventName][key];
                for(var i in arr){
                    if(arr[i].element != option.element) continue;
                    delete arr[i];
                }
            }
        }
    }
}